<template>
  <div>
    <vxe-toolbar>
      <template #buttons>
        <vxe-button @click="demo1.allAlign = 'left'">居左</vxe-button>
        <vxe-button @click="demo1.allAlign = 'center'">居中</vxe-button>
        <vxe-button @click="demo1.allAlign = 'right'">居右</vxe-button>
      </template>
    </vxe-toolbar>

    <h3>合并行</h3>
    <vxe-table border show-footer :footer-method="footerMethod1" height="500" :column-config="{ resizable: true }" :align="demo1.allAlign" :merge-cells="demo1.mergeCells" :data="demo1.tableData">
      <vxe-column type="seq" width="60"></vxe-column>
      <vxe-column field="name" title="Name"></vxe-column>
      <vxe-column field="role" title="Role"></vxe-column>
      <vxe-column field="sex" title="Sex"></vxe-column>
      <vxe-column field="age" title="Age"></vxe-column>
    </vxe-table>

    <h3>合并行 merge-method</h3>
    <vxe-table border height="500" :column-config="{ resizable: true }" :scroll-y="{ enabled: false }" :span-method="mergeRowMethod" :data="demo3.tableData">
      <vxe-column type="seq" width="60"></vxe-column>
      <vxe-column field="key" title="Key"></vxe-column>
      <vxe-column field="content" title="Translate"></vxe-column>
      <vxe-column
        field="language"
        title="Language"
        :filters="[
          { label: '中文', value: 'zh_CN' },
          { label: 'English', value: 'en_US' },
        ]"
      ></vxe-column>
    </vxe-table>

    <h3>合并列</h3>
    <vxe-table border height="500" :column-config="{ resizable: true }" :scroll-y="{ enabled: false }" :span-method="colspanMethod" :data="demo2.tableData">
      <vxe-column type="seq" width="60"></vxe-column>
      <vxe-column field="name" title="Name"></vxe-column>
      <vxe-column field="role" title="Role"></vxe-column>
      <vxe-column field="sex" title="Sex"></vxe-column>
      <vxe-column field="age" title="Age"></vxe-column>
    </vxe-table>
  </div>
</template>

<script setup lang="ts">
import { reactive } from 'vue'
import { VxeTablePropTypes } from 'vxe-table'

const demo1 = reactive({
  allAlign: null as VxeTablePropTypes.Align,
  tableData: [
    { id: 10001, name: 'Test1', nickname: 'T1', role: 'Develop', sex: 'Man', age: 28, address: 'test abc' },
    { id: 10002, name: 'Test2', nickname: 'T2', role: 'Test', sex: 'Women', age: 22, address: 'Guangzhou' },
    { id: 10003, name: 'Test3', nickname: 'T3', role: 'PM', sex: 'Man', age: 32, address: 'Shanghai' },
    { id: 10004, name: 'Test4', nickname: 'T4', role: 'Designer', sex: 'Women', age: 23, address: 'test abc' },
    { id: 10005, name: 'Test5', nickname: 'T5', role: 'Develop', sex: 'Women', age: 30, address: 'Shanghai' },
    { id: 10006, name: 'Test6', nickname: 'T6', role: 'Designer', sex: 'Women', age: 21, address: 'test abc' },
    { id: 10007, name: 'Test7', nickname: 'T7', role: 'Test', sex: 'Man', age: 29, address: 'test abc' },
    { id: 10008, name: 'Test8', nickname: 'T8', role: 'Develop', sex: 'Man', age: 35, address: 'test abc' },
  ],
  mergeCells: [
    { row: 1, col: 1, rowspan: 3, colspan: 3 },
    { row: 5, col: 0, rowspan: 2, colspan: 2 },
  ] as VxeTablePropTypes.MergeCells,
})

// 合并行-method
const demo3 = reactive({
  tableData: [
    { id: 10001, key: 'app.label.name', content: '名称', language: 'zh_CN' },
    { id: 10002, key: 'app.label.name', content: 'Name', language: 'en_US' },
    { id: 10003, key: 'app.label.sex', content: '性别', language: 'zh_CN' },
    { id: 10004, key: 'app.label.sex', content: 'Sex', language: 'en_US' },
    { id: 10005, key: 'app.label.age', content: '年龄', language: 'zh_CN' },
    { id: 10006, key: 'app.label.age', content: 'Age', language: 'en_US' },
    { id: 10007, key: 'app.label.role', content: '角色', language: 'zh_CN' },
    { id: 10008, key: 'app.label.role', content: 'Role', language: 'en_US' },
    { id: 10009, key: 'app.label.address', content: '地址', language: 'zh_CN' },
    { id: 10010, key: 'app.label.address', content: 'Address', language: 'en_US' },
    { id: 10011, key: 'app.label.nickname', content: '昵称', language: 'zh_CN' },
    { id: 10012, key: 'app.label.nickname', content: 'Nickname', language: 'en_US' },
  ],
})

// 通用行合并函数（将相同多列数据合并为一行）
const mergeRowMethod: VxeTablePropTypes.SpanMethod = ({ row, _rowIndex, column, visibleData }) => {
  const fields = ['key']
  const cellValue = row[column.property]
  if (cellValue && fields.includes(column.property)) {
    const prevRow = visibleData[_rowIndex - 1]
    let nextRow = visibleData[_rowIndex + 1]
    if (prevRow && prevRow[column.property] === cellValue) {
      return { rowspan: 0, colspan: 0 }
    } else {
      let countRowspan = 1
      while (nextRow && nextRow[column.property] === cellValue) {
        nextRow = visibleData[++countRowspan + _rowIndex]
      }
      if (countRowspan > 1) {
        return { rowspan: countRowspan, colspan: 1 }
      }
    }
  }
}

// 合并列
const demo2 = reactive({
  tableData: [
    { id: 10001, name: 'Test1', nickname: 'T1', role: 'Develop', sex: 'Man', age: 28, address: 'test abc' },
    { id: 10002, name: 'Test2', nickname: 'T2', role: 'Test', sex: 'Women', age: 22, address: 'Guangzhou' },
    { id: 10003, name: 'Test3', nickname: 'T3', role: 'PM', sex: 'Man', age: 32, address: 'Shanghai' },
    { id: 10004, name: 'Test4', nickname: 'T4', role: 'Designer', sex: 'Women', age: 23, address: 'test abc' },
    { id: 10005, name: 'Test5', nickname: 'T5', role: 'Develop', sex: 'Women', age: 30, address: 'Shanghai' },
    { id: 10006, name: 'Test6', nickname: 'T6', role: 'Designer', sex: 'Women', age: 21, address: 'test abc' },
    { id: 10007, name: 'Test7', nickname: 'T7', role: 'Test', sex: 'Man', age: 29, address: 'test abc' },
    { id: 10008, name: 'Test8', nickname: 'T8', role: 'Develop', sex: 'Man', age: 35, address: 'test abc' },
  ],
})
const colspanMethod: VxeTablePropTypes.SpanMethod = ({ _rowIndex, columnIndex }) => {
  if (_rowIndex % 2 === 0) {
    if (columnIndex === 2) {
      return { rowspan: 1, colspan: 2 }
    } else if (columnIndex === 3) {
      return { rowspan: 0, colspan: 0 }
    }
  }
}

// 表尾合并行
const footerData = [['合计', '2', '44', '67', '-']]
const footerMethod: VxeTablePropTypes.FooterMethod = () => {
  // 返回一个二维数组的表尾合计
  return footerData
}
// 表尾合并行
const meanNum = (list: any[], field: string) => {
  let count = 0
  list.forEach(item => {
    count += Number(item[field])
  })
  return count / list.length
}

const sumNum = (list: any[], field: string) => {
  let count = 0
  list.forEach(item => {
    count += Number(item[field])
  })
  return count
}

const footerMethod1: VxeTablePropTypes.FooterMethod = ({ columns, data }) => {
  const means: any[] = []
  const sums: any[] = []
  const others: any[] = []
  columns.forEach((column, columnIndex) => {
    if (columnIndex === 0) {
      means.push('平均')
      sums.push('和值')
      others.push('其他')
    } else {
      let meanCell: any = null
      let sumCell: any = null
      let otherCell = '-'
      switch (column.property) {
        case 'age':
        case 'amount':
          meanCell = meanNum(data, column.property)
          sumCell = sumNum(data, column.property)
          break
        case 'sex':
          otherCell = '无'
          break
      }
      means.push(meanCell)
      sums.push(sumCell)
      others.push(otherCell)
    }
  })
  // 返回一个二维数组的表尾合计
  return [means, sums, others]
}
</script>

<style scoped></style>
